package org.springblade.modules.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.springblade.common.annotation.FrontEndShow;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.ReflectUtil;
import org.springblade.modules.enterprise.entity.*;
import org.springblade.modules.enterprise.service.*;
import org.springblade.modules.enterprise.vo.BackIndexVO;
import org.springblade.modules.enterprise.vo.PolicyApplyStatusChartsVO;
import org.springblade.modules.enterprise.vo.ReadForAppVO;
import org.springblade.modules.enterprise.vo.ServiceTypeChartsVO;
import org.springframework.stereotype.Service;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

@Slf4j
@AllArgsConstructor
@Service
public class IndexServiceImpl implements IIndexService {
	// 产品采购
	public static final int PROD_PROC = 0;
	// 产品发布
	public static final int PROD_RELE = 1;
	private ICompanyBasicInfService companyBasicInfService;

	// 政策查阅总次数
	private IContentService contentService;

	private IPolicyApplyService policyApplyService;

	private IEsmsTalentServiceService talentServiceService;

	private IEsmsCryService cryService;

	private IEsmsProductCloudService productCloudService;

	private IProblemService problemService;

	@Override
	public ReadForAppVO getReadForApp(String account) {
		ReadForAppVO readForAppVO = new ReadForAppVO();
		//问题反馈
		QueryWrapper<Problem>problemQueryWrapper=new QueryWrapper<>();
		List<Problem>problemList=problemService.list(problemQueryWrapper.eq("read_status",1).eq("enterprise_account",account));
		if (problemList.size()>0)
			readForAppVO.setProblemCount(problemList.size());
		//融资贷款
		QueryWrapper<EsmsCry>esmsCryQueryWrapper=new QueryWrapper<>();
		List<EsmsCry>esmsCryList=cryService.list(esmsCryQueryWrapper.eq("read_status",1).eq("enterprise_account",account));
		if (esmsCryList.size()>0)
			readForAppVO.setCryCount(esmsCryList.size());
		//人才服务
		QueryWrapper<EsmsTalentService>esmsTalentServiceQueryWrapper=new QueryWrapper<>();
		List<EsmsTalentService>esmsTalentServiceList=talentServiceService.list(esmsTalentServiceQueryWrapper.eq("read_status",1).eq("service_type","人才服务").eq("enterprise_account",account));
		if (esmsTalentServiceList.size()>0)
			readForAppVO.setTalentCount(esmsTalentServiceList.size());
		//专业服务
		esmsTalentServiceQueryWrapper=new QueryWrapper<>();
		esmsTalentServiceList=talentServiceService.list(esmsTalentServiceQueryWrapper.eq("read_status",1).eq("service_type","专业服务").eq("enterprise_account",account));
		if (esmsTalentServiceList.size()>0)
			readForAppVO.setProfessionalCount(esmsTalentServiceList.size());
		//创业服务
		esmsTalentServiceQueryWrapper=new QueryWrapper<>();
		esmsTalentServiceList=talentServiceService.list(esmsTalentServiceQueryWrapper.eq("read_status",1).eq("service_type","创业服务").eq("enterprise_account",account));
		if (esmsTalentServiceList.size()>0)
			readForAppVO.setEntrepreneurshipCount(esmsTalentServiceList.size());
		//上市辅导
		esmsTalentServiceQueryWrapper=new QueryWrapper<>();
		esmsTalentServiceList=talentServiceService.list(esmsTalentServiceQueryWrapper.eq("read_status",1).eq("service_type","上市辅导").eq("enterprise_account",account));
		if (esmsTalentServiceList.size()>0)
			readForAppVO.setListedCount(esmsTalentServiceList.size());
		//产品发布
		QueryWrapper<EsmsProductCloud>esmsProductCloudQueryWrapper=new QueryWrapper<>();
		List<EsmsProductCloud>esmsProductCloudList=productCloudService.list(esmsProductCloudQueryWrapper.eq("read_status",1).eq("function_type",1).eq("enterprise_account",account));
		if (esmsProductCloudList.size()>0)
			readForAppVO.setProductReleaseCount(esmsProductCloudList.size());
		//产品采购
		esmsProductCloudQueryWrapper=new QueryWrapper<>();
		esmsProductCloudList=productCloudService.list(esmsProductCloudQueryWrapper.eq("read_status",1).eq("function_type",0).eq("enterprise_account",account));
		if (esmsProductCloudList.size()>0)
			readForAppVO.setProductsPurchasingCount(esmsProductCloudList.size());

		return readForAppVO;
	}
	@Override
	public BackIndexVO getIndexInfo(String account) {
		BackIndexVO backIndexVO = new BackIndexVO();
		// 1)企业信息完善度
		CompanyBasicInf companyBasicInf = companyBasicInfService.getOne(new QueryWrapper<CompanyBasicInf>().eq("user_name", account));
		if (ObjectUtil.isNotEmpty(companyBasicInf)) {
			backIndexVO.setCompanyInfoCompletion(imputedObjPropProportion(companyBasicInf));
			// 2)政策订阅总数
			backIndexVO.setPolicySubCount(getPolicySubCount(companyBasicInf));
			// 3)政策查阅总次数
			backIndexVO.setPolicyLookCount(companyBasicInf.getPolicyLookCount());
			// 4)申报政策总数
			backIndexVO.setPolicyApplyCount(getPolicyApplyCount(companyBasicInf));
			// 5)企业申报政策各状态审核统计
			backIndexVO.setPolicyApplyStatusCharts(getPolicyApplyStatusCharts(companyBasicInf));
			// 6)需求发布总数
			backIndexVO.setDemandCount(getDemandCount(companyBasicInf));
			// 7)各类型服务统计
			backIndexVO.setServiceTypeCharts(getServiceTypeCharts(companyBasicInf));

			// 8)融资申请总数
			backIndexVO.setLenderFinancingApplyCount(getLenderFinancingApplyCount(companyBasicInf));
			// 9)产品发布总条数
			backIndexVO.setProdReleaseCount(getProCount(companyBasicInf, PROD_RELE));
			// 10)产品采购总数
			backIndexVO.setProdProcCount(getProCount(companyBasicInf, PROD_PROC));
			// 11)问题反馈总数
			backIndexVO.setProblemCount(getProblemCount(companyBasicInf));
		}
		return backIndexVO;
	}
	@Override
	public float getIndexInfoForApp(String account) {
		BackIndexVO backIndexVO = new BackIndexVO();
		// 1)企业信息完善度
		CompanyBasicInf companyBasicInf = companyBasicInfService.getOne(new QueryWrapper<CompanyBasicInf>().eq("user_name", account));
		if (ObjectUtil.isNotEmpty(companyBasicInf)) {
			return imputedObjPropProportion(companyBasicInf);
		}
		return 0;
	}

	private int getProblemCount(CompanyBasicInf companyBasicInf) {
		return problemService.count(new QueryWrapper<Problem>().eq("enterprise_account", companyBasicInf.getUserName()));
	}

	private List<ServiceTypeChartsVO> getServiceTypeCharts(CompanyBasicInf companyBasicInf) {
		return talentServiceService.getServiceTypeCharts(companyBasicInf.getUserName());
	}

	private List<PolicyApplyStatusChartsVO> getPolicyApplyStatusCharts(CompanyBasicInf companyBasicInf) {
		return policyApplyService.getPolicyApplyStatusCharts(companyBasicInf.getUserName());
	}

	private int getProCount(CompanyBasicInf companyBasicInf, int type) {
		QueryWrapper<EsmsProductCloud> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("enterprise_account", companyBasicInf.getUserName())
			.eq("function_type", type);
		return productCloudService.count(queryWrapper);
	}

	private int getLenderFinancingApplyCount(CompanyBasicInf companyBasicInf) {
		QueryWrapper<EsmsCry> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("enterprise_account", companyBasicInf.getUserName());
		return cryService.count(queryWrapper);
	}

	private int getDemandCount(CompanyBasicInf companyBasicInf) {
		QueryWrapper<EsmsTalentService> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("enterprise_account", companyBasicInf.getUserName());
		return talentServiceService.count(queryWrapper);
	}

	private int getPolicyApplyCount(CompanyBasicInf companyBasicInf) {
		QueryWrapper<PolicyApply> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("enterprise", companyBasicInf.getUserName());
		return policyApplyService.count(queryWrapper);
	}

	private int getPolicySubCount(CompanyBasicInf companyBasicInf) {
		String area = companyBasicInf.getArea();
		Integer[] areas = new Integer[0];
		if (Func.isNoneBlank(area)) {
			areas = (Integer[]) ConvertUtils.convert(area.split(","),Integer.class);
		}
		String prod = companyBasicInf.getProd();
		Integer[] prods = new Integer[0];
		if (Func.isNotBlank(prod)) {
			prods = (Integer[]) ConvertUtils.convert(prod.split(","),Integer.class);
		}

		String industry = companyBasicInf.getIndustry();
		Integer[] industrys = new Integer[0];
		if (Func.isNotBlank(industry)) {
			industrys = (Integer[]) ConvertUtils.convert(industry.split(","),Integer.class);
		}

		Query query = new Query();
		query.setCurrent(0);
		query.setSize(0);
		IPage<Content> page = contentService.selectSubContentPage(Condition.getPage(query), areas, prods, industrys);
		return (int)page.getTotal();
	}

	private float imputedObjPropProportion(CompanyBasicInf companyBasicInf) {
		try {
			// 获取类的属性描述
			PropertyDescriptor[] propertiesHelper = ReflectUtil.getPropertiesHelper(companyBasicInf.getClass(), true, false);
			// 获取企业信息前端展示并需要填写的总字段数
			float filedCount = 0;
			float hasValue = 0;
			for (PropertyDescriptor propertyDescriptor : propertiesHelper) {
				// 获取字段上面的FrontEndShow注解
				FrontEndShow frontEndShow = ReflectUtil.getAnnotation(companyBasicInf.getClass(), propertyDescriptor.getName(), FrontEndShow.class);
				if (ObjectUtil.isNotEmpty(frontEndShow)) {
					// 注解不为空
					filedCount++;
					Method readMethod = propertyDescriptor.getReadMethod();
					// 属性不为空
					if (Func.isNotEmpty(readMethod.invoke(companyBasicInf))) {
						hasValue++;
					}
				}
			}
			// 计算空属性占比
			return hasValue / filedCount;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
}
